package com.chinasoft.service.Impl;

import com.chinasoft.mapper.GoodlistMapper;
import com.chinasoft.pojo.GoodCount;
import com.chinasoft.pojo.Usergood;
import com.chinasoft.service.UsergoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class UsergoodServiceImpl implements UsergoodService {
    @Autowired
    GoodlistMapper usergoodMapper;
    @Override
    public List<Usergood> find(String username) {
        List<Usergood> usergoods = usergoodMapper.find(username);
        List<Usergood> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(30);//减去一个小时
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (Usergood teacher : usergoods) {
            if ("0".equals(teacher.getDel()) &&teacher.getEndtime().compareTo(dateTime) < 0 ) {
                //当del=0且时间小于dateTime或者人数已满时
                String del = "1";
                usergoodMapper.updateDel(teacher.getId(), del);
            }
            result.add(teacher);
        }
        return result;
    }

    @Override
    public boolean add(Usergood usergood) {
        int num = usergoodMapper.insert(usergood);
        if(num ==1){
            return  true;
        }else{
            return false;
        }
    }

    @Override
    public List<Usergood> findUpdate(String username) {
        List<Usergood> usergoods = usergoodMapper.find(username);
        List<Usergood> result = new ArrayList<>();
        for (Usergood teacher : usergoods) {
            if ("0".equals(teacher.getDel()) && !"体验套餐".equals(teacher.getGoodname()) ) {
                //当del=0且套餐名称不为体验套餐时
                String del = "1";
                usergoodMapper.updateDel(teacher.getId(), del);
            }
            result.add(teacher);
        }
        return result;
    }

    @Override
    public List<GoodCount> find() {
        return usergoodMapper.findlist();
    }

    @Override
    public boolean updateUse(Integer id) {
        String del = "1";
        int i = usergoodMapper.updateDel(id, del);
        if (i ==1){
            return true;
        } else{
            return false;
        }
    }


}
